<template>
  <div class='u-sys_balanceOfProject_goodsDialog'>
    <el-dialog ref='dialog'
      title="商品信息"
      :visible.sync="dialogVisible"
      :before-close="() => {this.dialogVisible = false}"
    >
      <el-form :inline='true' label-width='120'>
        <el-form-item label='商品名称：'>
          <span @click='showSkuDialog'>选择商品</span>
        </el-form-item>
        <el-form-item label='进货单价：'>
          <el-input v-model.trim='data.price' type='number' />
        </el-form-item>
      </el-form>
      <div style='margin-top: 15px;'>
        <el-button type='primary' @click='add'>{{ isAdd ? '添加' : '修改' }}</el-button><el-button type='primary' @click='dialogVisible = false'>关闭</el-button>
      </div>
    </el-dialog>
    <sku-dialog ref='skuDialog' @getData='getData' :selectSkuIds='selectSkuIds' :isNotUseSkuId='true' />
  </div>
</template>

<script>
import skuDialog from '@/module/invent/components/skuDialog'

export default {
  name: 'u-sys_balanceOfProject_goodsDialog',

  props: ['selectSkuIds', 'id'],

  components: {
    skuDialog
  },

  data() {
    return {
      dialogVisible: false,
      data: {
        id: '',
        price: ''
      },
      goodsId: '',
      tableKeys: [],
      isAdd: true,
      selectIndex: -1
    }
  },

  methods: {
    $show(type, index) {
      this.isAdd = type !== 'edit'
      if (this.isAdd) {
        this.$refs.skuDialog.clearCache()
      }
      this.selectIndex = this.isAdd ? -1 : index
      this.dialogVisible = true
    },

    reset() {},

    showSkuDialog(type, index) {
      this.$refs.skuDialog.$show()
    },

    removeItem(i) {
      this.data.splice(i, 1)
    },

    emitData() {
      const data = {}
      this.$emit('update', data)
    },

    async add() {
      // 检查是否有价格
      let msg = ''
      if (!this.data.price) {
        msg = '请输入价格'
      }
      if (msg) {
        return this.__notify('警告', msg, 'warning')
      }
      const { code } = await this.$api.inventSetCostGoods({
        aFirmId: this.aFirmId,
        aGoodsId: this.data.id,
        aValue: this.data.price,
        aCostId: this.id
      })
      if (code === 0) {
        this.__notify('提示', '添加成功')
        this.$emit('update')
        this.dialogVisible = false
        this.data.id = ''
        this.data.price = ''
      }
    },

    set(data) {
      // 拼装商品数据
      this.data = JSON.parse(JSON.stringify(data))
      // 设置 skuDialog 的 cacheObj
      this.$refs.skuDialog.setCache(this.data)
    },

    getData(data) {
      if (data.id === this.data.id) return false
      this.data = {
        ...data
      }
    }
  }
}
</script>

<style lang='less'>

</style>